<template>
  <div class="search-container">
    <NavBar title="搜索朋友/群聊" left-arrow @click-left="handleBack" />
    <Search v-model="searchValue" @search="onSearch" @clear="onClear" />
    <div class="search-list">
      <div class="search-item" v-for="item in searchList" :key="item._id">
        <!-- 群 -->
        <div class="item-wrap" v-if="item.creator">
          <div class="search-item-avatar">
            <img :src="item.creator.avatar" alt="" />
          </div>
          <div class="search-item-name">
            {{ item.name }}
            <span class="search-item-desc">{{ item.description }}</span>
          </div>
          <div class="search-item-btn" v-if="!isJoined">
            <van-button type="primary" @click="handleJoinGroup(item._id)">加入群聊</van-button>
          </div>
          <div class="search-item-btn" v-else>
            <van-button type="default" @click="handleGoGroup(item._id)">进入群聊</van-button>
          </div>
        </div>
        <!-- 用户 -->
        <div class="item-wrap" v-else>
          <div class="search-item-avatar">
            <img :src="item.avatar" alt="" />
          </div>
          <div class="search-item-name">
            {{ item.username }}
          </div>
          <div class="search-item-btn">
            <van-button type="default" @click="handleViewHome(item._id)">查看主页</van-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { searchApi } from '@/api/search'
import { useRouter } from 'vue-router'
import NavBar from '@/components/NavBar.vue'
import Search from '@/components/Search.vue'
import { chatService } from '@/utils/webSocket'

const user = JSON.parse(localStorage.getItem('user'))

const router = useRouter()
const searchValue = ref('')

const searchList = ref([])

// 判断当前用户是否已加入群聊
const isJoined = computed(() => {
  if (searchValue.value) {
    return searchList.value.map((item) => item.members.includes(user.id))
  }
  return []
})
const handleBack = () => {
  router.back()
}
const handleSearch = async () => {
  const res = await searchApi.searchUserOrRoom({ keyword: searchValue.value })
  searchList.value = res
}
const onSearch = async () => {
  searchList.value = []
  await handleSearch()
}
const onClear = () => {
  searchValue.value = ''
  searchList.value = []
}
// 查看主页
const handleViewHome = (id) => {
  router.push(`/user/${id}`)
}
// 加入群聊
const handleJoinGroup = (id) => {
  chatService.sendMessage({
    roomId: id,
    content: '加入群聊',
    type: 'joinRoom',
  })
  router.push({
    path: `/chat/detail/${id}`,
    query: {
      type: 'group',
    },
  })
}
// 进入群聊
const handleGoGroup = (id) => {
  router.push({
    path: `/chat/detail/${id}`,
    query: {
      type: 'group',
    },
  })
}
</script>

<style scoped>
:deep(.van-nav-bar) {
  background-color: var(--bg-color-light);
}
.search-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: var(--bg-color-light);
}
.search-list {
  flex: 1;
  overflow-y: auto;
}
.search-item {
  display: flex;
  padding: 10px;
  border-bottom: 1px solid var(--border-color-light);
}
.item-wrap {
  display: flex;
  align-items: center;
  width: 100%;
}
.search-item-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  overflow: hidden;
}
.search-item-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.search-item-name {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-evenly;
  margin-left: 10px;
  font-size: var(--font-size-base);
  color: var(--font-color-black);
}
.search-item-desc {
  font-size: var(--font-size-xs);
  color: var(--font-color-gray);
}
.search-item-btn {
  margin-left: auto;
}
:deep(.van-button) {
  height: 30px;
  font-size: var(--font-size-sm);
  border-radius: var(--radius-md);
}
</style>
